<template>
  <!-- 客户类型页面 -->
  <div class="app-container">
    <el-form :model="queryParams" :inline="true" label-width="70px">
      <el-form-item label="门店标签">
        <el-input
          v-model="queryParams.customerType"
          placeholder="请输入门店标签"
          size="small"
          style="width: 150px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-button
        type="primary"
        plain
        icon="el-icon-plus"
        size="mini"
        @click="handleAddOrder"
        >新增</el-button
      >
      <el-button
        type="danger"
        plain
        icon="el-icon-refresh"
        size="mini"
        @click="handleClearCache"
        >刷新</el-button
      >
    </el-row>
    <el-table v-loading="loading" :data="typeList">
      <el-table-column label="序号" align="center" type="index" width="60" />
      <el-table-column label="一级标签" align="center" prop="customerType" />
      <el-table-column label="二级标签" align="center" prop="labelInfo" />
      <el-table-column
        label="创建时间"
        align="center"
        :show-overflow-tooltip="true"
        prop="createTime"
      >
      </el-table-column>
      <!-- <el-table-column
        label="备注"
        align="center"
        :show-overflow-tooltip="true"
        prop="remarks"
      >
      </el-table-column> -->

      <el-table-column
        label="操作"
        align="center"
        :show-overflow-tooltip="true"
        prop=""
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleGoEdit(scope.row)"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 新增门店标签 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body  @closed="cancel"  >
      <div class="xian_"></div>
      <el-form
        ref="form"
        :model="ruleForm"
        :rules="rulesForm"
        label-width="80px"
        :hide-required-asterisk="true"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item
              label="一级标签"
              prop="parentName"
              label-width="100px"
            >
              <el-input
                v-model="ruleForm.parentName"
                placeholder="请输入一级标签"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="二级标签" class="twoLabel">
              <el-input
                v-model="two_Lable"
                placeholder="请输入二级标签"
                @keyup.enter.native="handleAddLable"
              />
              <span class="sure_" @click="handleAddLable">确认</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="二级标签集合" label-width="100px">
              <div class="content_label">
                <div
                  class="item_"
                  v-for="(item, index) in childrenList"
                  :key="index"
                >
                  <span class="text">{{ item }}</span>
                  <span
                    @click="handleDelLable(index)"
                    class="el-icon-close"
                  ></span>
                </div>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- <el-row>
          <el-col :span="24">
            <el-form-item label="备注" label-width="100px">
              <el-input
                v-model="ruleForm.remarks"
                type="textarea"
                placeholder="请输入内容"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button
          type="primary"
          @click="submitFormAdd('form')"
          v-if="title == '新增门店标签'"
          >确 定</el-button
        >
        <el-button
          type="primary"
          @click="submitFormEdit('form')"
          v-if="title == '编辑门店标签'"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  customerTypeList,
  customerTypeAdd,
  customerTypeEdit,
} from "@/api/sale/customer";
export default {
  data() {
    return {
      rowData: {}, //当前行数据
      two_Lable: "",
      title: "",
      openDetail: false,
      // 遮罩层
      loading: true,
      // 选中数组
      total: 0,
      // 订单列表
      typeList: [],
      // 是否显示弹出层
      open: false,
      // 搜索状态字典
      statusOptions: [], //支付类型
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerType: null,
        parentId: 0,
      },
      ruleForm: {
        parentName: "",
        tag: []
      },
      childrenList : [],
      rulesForm: {
        parentName: [
          { message: "请输入一级标签", trigger: "blur", required: true },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleAddLable() {
      if (this.two_Lable == "") {
        return false;
      }
      //添加二级标签
      this.childrenList.push(this.two_Lable);
      this.childrenList = [...new Set(this.childrenList)];
      setTimeout(() => {
        this.two_Lable = "";
      }, 100);
    },
    handleDelLable(index) {
      //删除二级标签
      this.childrenList.splice(index, 1);
    },
    submitFormAdd(formName) { //新增标签
      this.ruleForm.tag = this.childrenList.join(",")
      this.$refs[formName].validate((valid) => {
        if (valid) {
          customerTypeAdd({ tagJson: JSON.stringify(this.ruleForm) }).then(
            (res) => {
              if (res.code == 200) {
                this.$message({
                  message: "新增门店标签成功",
                  type: "success",
                });
                this.open = false;
                setTimeout(() => {
                  this.getList();
                  this.ruleForm = {
                    parentName: "",
                    tag: [],
                    // remarks: "",
                  };
                }, 100);
              }
            }
          );
        } else {
          return false;
        }
      });
    },
    submitFormEdit(formName) {  //编辑门店标签
     this.ruleForm.tag = this.childrenList.join(",");
      this.$refs[formName].validate((valid) => {
        if (valid) {
          customerTypeEdit({ tagJson: JSON.stringify(this.ruleForm) }).then(
            (res) => {
              if (res.code == 200) {
                this.getList();
                this.$message({
                  message: "编辑门店标签成功",
                  type: "success",
                });
                this.open = false;
                setTimeout(() => {
                  this.childrenList.length = 0;
                  this.getList();
                  this.ruleForm = {
                    parentName: "",
                    typeId: "",
                    tag : ""
                  };
                }, 100);
              }
            }
          );
        } else {
          return false;
        }
      });
    },
    cancel() {
      this.ruleForm = {
        tag: "",
        parentName: "",
        // remarks: "",
        typeId: "",
      };
      this.open = false;
      this.childrenList = []
    },
    handleGoEdit(row) {
      //编辑标签
      this.rowData = row;
      this.ruleForm.parentName = row.customerType;
      // this.ruleForm.remarks = row.remarks;
      this.ruleForm.typeId = row.typeId;
      this.childrenList = row.labelInfoList;
      this.title = "编辑门店标签";
      this.open = true;
    },
    handleGoDetail() {
      //详情
      this.title = "门店标签详情";
      this.openDetail = true;
    },
    handleClearCache() {
      window.location.reload();
    },
    handleAddOrder() {
      //新增
      this.ruleForm.parentName = "";
      this.ruleForm.tag =""
      this.childrenList = [];
      this.title = "新增门店标签";
      this.open = true;
    },
    /** 查询字典类型列表 */
    getList(index) {
      this.loading = true;
      customerTypeList(this.queryParams).then((response) => {
        this.typeList = response.rows;
        this.typeList = response.rows.map((item) => {
          var labelInfo = [];
          item.tagList.forEach((v) => {
            labelInfo.push(v.customerType);
          });
          item.labelInfoList = labelInfo;
          item.labelInfo = labelInfo.join("，");
          return item;
        });
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      //重置事件
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        customerType: null,
      };
      this.getList();
    },
  },
};
</script>
<style lang="scss" scoped>
.xian_ {
  height: 1px;
  width: 600px;
  background-color: #eee;
  margin-left: -20px;
  margin-top: -25px;
  margin-bottom: 30px;
}
.el-input--medium {
  margin-bottom: 10px !important;
}
.twoLabel {
  position: relative;
  .sure_ {
    position: absolute;
    right: 0;
    background: #1890ff;
    height: calc(100% - 10px);
    padding: 0 5px;
    color: #fff;
    font-size: 10px;
    cursor: pointer;
  }
}
.content_label {
  display: flex;
  flex-wrap: wrap;
  padding: 10px;
  box-sizing: border-box;
  width: 100%;
  min-height: 100px;
  border: 1px solid #DCDFE6;
  border-radius: 6px;
  .item_ {
    margin-right: 6px;
    padding: 0 10px;
    box-sizing: border-box;
    line-height: 30px;
    height: 30px;
    background: #f3f4f5;
    border-radius: 10px;
    cursor: pointer;
    display: flex;
    align-items: center;
    margin-top: 3px;
    margin-bottom: 3px;
    .text {
      margin-right: 5px;
    }
  }
}
.el-form-item__error {
  margin-bottom: -6px !important;
}
.el-range-editor {
  width: 100% !important;
}
  .dialog-footer {
    display: flex;
    justify-content: center;
  }
.app-container {

  .el-col-11 {
    width: 100%;
  }
  .el-form-item__content {
    position: relative !important;
    span {
      font-size: 16px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      color: #333333;
      position: absolute;
      top: 1px;
      right: 12px;
    }
  }
  .mb8 {
    .title {
      font-size: 20px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      color: #333333;
    }
    .create {
      margin-right: 20 px;
      float: right;
    }
  }
}
</style>